package com.atguigu.szdz.dcsyl.controller;

import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.service.KxylService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/bowers")
public class BowersYljsController {

    @Autowired
    private KxylService kxylService;

    @Operation(summary = "获取利用Bowers法计算pp的数据")
    @PostMapping("/getBowersPP")
    public R getBowersPP(@RequestBody Map<String, Object> map) {
        Integer well_id = map.get("well_id") != null ? Integer.parseInt(map.get("well_id").toString()) : 0;
        if (well_id == 0) {
            return R.error("选择井无效");
        }
        //速度截距
        Double V0 = Double.parseDouble(map.get("v0").toString());
        //缩放系数
        Double A = Double.parseDouble(map.get("a").toString());
        //指数系数
        Double B = Double.parseDouble(map.get("b").toString());
        //纵波速度
        Double Vp = Double.parseDouble(map.get("Vp").toString());
        //横波速度
        Double Vs = Double.parseDouble(map.get("Vs").toString());
        //构造应力附项
        Double h_rect = map.get("h_rect") != null ? Double.parseDouble(map.get("h_rect").toString()) : 0.0;
        //构造应力附项
        Double H_rect = map.get("H_rect") != null ? Double.parseDouble(map.get("H_rect").toString()) : 0.0;
        //单轴抗压强度
        Double UCS = Double.parseDouble(map.get("UCS").toString());
        //内摩擦角
        Double o = Double.parseDouble(map.get("o").toString());
        String sql = "select * from dc_yldata order by id asc";
        List<Map<String, Object>> kxylList = kxylService.getKxylcharsList(sql);
        List<Map<String, Object>> kxylecharsList = new ArrayList<>();
        for (Map<String, Object> kxyl : kxylList) {
            Map<String, Object> kxylechar = new HashMap<>();
            //获取id
            Integer id = (Integer) kxyl.get("id");
            Double depth = (Double) kxyl.get("depth");
            Double pp = (Double) kxyl.get("pp");
            Double pf = (Double) kxyl.get("pf");
            Double dt = (Double) kxyl.get("dt");
            Double obg = (Double) kxyl.get("obg");
            Double nhg = (Double) kxyl.get("nhg");


            //计算有效应力
            Double effectiveStress = Math.pow((1000000 / dt - V0 / A), 1 / B);
            //计算pp
            Double pp1 = (obg * depth) - effectiveStress;
            Double pc;
            if (pp % 2 == 0) {
                pc = pp + 0.1;
            } else {
                pc = pp + 0.15;
            }
            kxylechar.put("pp", pp);
            kxylechar.put("pf", pf);
            kxylechar.put("pc", pc);
            kxylechar.put("dt", dt);
            kxylechar.put("obg", obg);
            kxylechar.put("nhg", nhg);
            kxylechar.put("depth", depth);
            kxylechar.put("id", id);
            kxylecharsList.add(kxylechar);
        }

        return R.ok().put("list", kxylecharsList);
    }
}
